Ext.define('cfWeb.view.appAudit.AppAuditView', {
    extend: 'Ext.container.Container',
    alias: 'widget.appAuditView',
    overflowY: 'auto',
    overflowX: 'hidden',
    items: [
        {
            xtype: 'collapsibleBlock',
            title: '待审批的应用',
            items: [
                {
                    xtype: 'grid',
                    store: Ext.create('cfWeb.store.appAudit.AppAuditStore'),
                    columns: [
                        {text: '编号', dataIndex: 'id', hidden: true},
                        {text: '模板编号', dataIndex: 'templateId', hidden: true},
                        {text: '应用名称', dataIndex: 'appName', flex: .5},
                        {text: '域名', dataIndex: 'domain', flex: .5},
                        {text: '实例', dataIndex: 'instance', flex: .5},
                        {text: '内存', dataIndex: 'memory', flex: .5},
                        {text: '空间', dataIndex: 'space', flex:.5},
                        {text: '组织', dataIndex: 'org', flex:.5},
                        {text: '申请人', dataIndex: 'userName', flex:.5},
                        {text: '日期', dataIndex: 'date', flex: 1},
                        {
                            flex: 1.5,
                            text: '操作',
                            renderer: function (val, meta, rec) {
                                // generate unique id for an element
                                var id = Ext.id();
                                var id2 = Ext.id();
                                var store = this.getStore();
                                var state = rec.data.state;
                                Ext.defer(function () {
                                    if ((state == 0||state == 1)&&Ext.get(id)) {
                                        Ext.widget('button', {
                                            renderTo: id,
                                            text: '批准',
                                            scale: 'small',
                                            handler: function () {
                                                var auditStore = store;
                                                Ext.Ajax.request({
                                                    url: 'rest/audit/getone?id=' + rec.data.id,
                                                    async: false,
                                                    success: function(res) {
                                                        var data = Ext.decode(res.responseText).data;
                                                        //模拟数据
                                                        //data.serviceNum = 4; data.serviceAll = 100;
                                                        //data.memoryUsed = 1536; data.memoryAll = 20480;
                                                        rec.data.serviceUsage = data.serviceNum + '/' + data.serviceAll;
                                                        function trans(v) {
                                                            if (v < 1000) return v + 'M';
                                                            else return v/1024.0 + 'G';
                                                        }
                                                        rec.data.memoryUsage = trans(data.memoryUsed) + '/' + trans(data.memoryAll);
                                                        rec.data.envName = data.env;
                                                        rec.data.serName = '';
                                                        var sizeMap = {
                                                            small: cfWebLang.ServiceConfig.StorageSizeSmall,
                                                            medium: cfWebLang.ServiceConfig.StorageSizeMedium,
                                                            large: cfWebLang.ServiceConfig.StorageSizeLarge
                                                        };
                                                        for (var i = 0; i < data.stoList.length; i++) {
                                                            if (rec.data.serName) rec.data.serName += ', ';
                                                            rec.data.serName += data.stoList[i].serviceName;
                                                            if (data.stoList[i].type == 'select') {
                                                                rec.data.serName += '(' + data.stoList[i].serviceInstanceName + ')';
                                                            } else {
                                                                rec.data.serName += '(需创建-' + sizeMap[data.stoList[i].serviceSize] + ')';
                                                            }
                                                        }
                                                        var window = Ext.widget('appAuditCfWindow', {
                                                            langSet: {
                                                                'next': cfWebLang.Util.Next,
                                                                'prev': cfWebLang.Util.Prev,
                                                                'finish': cfWebLang.Util.Finish
                                                            }
                                                        });
                                                        window.data = rec.data;
                                                        window.auditStore = auditStore;

                                                        // 展示app信息的页
                                                        var appInfoView = Ext.widget('appInfoView');
                                                        window.add(appInfoView);
                                                        window.down('form[name=LoadTem.Form]').loadRecord(rec);
                                                        console.log(data);

                                                        var serviceListView = Ext.widget('serviceConfigListView');
                                                        window.add(serviceListView);

                                                        var dbStore = Ext.create('cfWeb.store.dataStorage.DatabaseTmpStore');
                                                        dbStore.on('load', function(store, records) {
                                                            console.log(records);
                                                            data.stoList.forEach(function(service) {
                                                                var serviceName = service.serviceName;
                                                                if (service.type == 'create') {
                                                                    serviceListView.down('#' + serviceName.toLowerCase() + 'FieldSet').show();
                                                                    records.forEach(function(record) {
                                                                        if (record.data.itemName.toLowerCase() === serviceName.toLowerCase()) {
                                                                            var serviceBroker = record.data.brokers;
                                                                            var brokers = null;
                                                                            for (var k = 0; k < serviceBroker.length; k++) {
                                                                                brokers = serviceBroker[k] + ',' + brokers;
                                                                            }
                                                                            console.log(brokers);
                                                                            var serviceBrokerStore = serviceListView.down('#' + serviceName.toLowerCase() + 'FieldSet').store;
                                                                            serviceBrokerStore.load({
                                                                                params: {
                                                                                    lguid: brokers
                                                                                },
                                                                                callback: function (re) {
                                                                                    var combo = serviceListView.down('combobox[name=' + serviceName.toLowerCase() + '.service]');
                                                                                    var storeServiceName = combo.getStore();
                                                                                    storeServiceName.loadData(re);
                                                                                    combo.setValue(storeServiceName.getAt(0).get('label'));
                                                                                }
                                                                            });
                                                                        }
                                                                    });
                                                                }
                                                            });
                                                        });

                                                        /*for (var i = 0; i < data.stoList.length; i++) {
                                                            var service = data.stoList[i];
                                                            var serviceName = service.serviceName;
                                                            if (serviceName == 'MySQL' ||
                                                                serviceName == 'Oracle'
                                                                ) {
                                                                var title;
                                                                if (service.type == 'create') {
                                                                    var size;
                                                                    if (service.serviceSize == 'large') {
                                                                        size = '大';
                                                                    } else if (service.serviceSize == 'medium') {
                                                                        size = '中';
                                                                    } else if (service.serviceSize == 'small') {
                                                                        size = '小';
                                                                    }
                                                                    title = '创建' + serviceName + '(' + size + ')';
                                                                } else {
                                                                    title = serviceName + '(' + service.serviceInstanceName + ')';
                                                                }
                                                                var stoListView = Ext.widget('serviceConfigView', {
                                                                    serType: cfWebLang.HomeTabView.dataStorageService,
                                                                    serName: serviceName,
                                                                    serNameTitle: title
                                                                });
                                                                var view = Ext.widget(serviceName, {
                                                                    title: serviceName,
                                                                    store: Ext.create('cfWeb.store.dataStorage.DatabaseServiceSelectStore')
                                                                });
                                                                view.store.load({
                                                                    params: {
                                                                        first: 1,
                                                                        second: service.serviceId
                                                                    }
                                                                });
                                                                if (service.type == 'create') {
                                                                    // 创建
                                                                } else {
                                                                    view.down('grid toolbar').hide();
                                                                    var store = view.down('grid').getStore();
                                                                    view.fireEvent('grow', view, 1);
                                                                    var d = {
                                                                        'appSource': cfWebLang.AppManageView.InputDataSourceName,
                                                                        'cloudSource': service.serviceInstanceName
                                                                    };
                                                                    store.add(d);
                                                                    console.log(view.down('grid').columns[1].editor);
                                                                    view.down('grid').columns[1].editor.readOnly = true;
                                                                }
                                                                stoListView.add(view);
                                                                window.add(stoListView);
                                                            }
                                                        }*/

                                                        window.show();
                                                    }
                                                });
                                            },

                                        });
                                    }
                                    if ((state == 0)&&Ext.get(id2)) {
                                        Ext.widget('button', {
                                            renderTo: id2,
                                            text: '拒绝',
                                            scale: 'small',
                                            handler: function () {
                                                Ext.Msg.show({
                                                    icon: Ext.Msg.QUESTION,
                                                    title: '确定操作',
                                                    msg: '是否确定拒绝' + rec.data.userName + '的申请？',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function (btn, text) {
                                                        if (btn == 'yes') {
                                                            Ext.Ajax.request({
                                                                type: 'rest',
                                                                url: 'rest/audit/disagree?id=' + rec.data.id,
                                                                success: function (res) {
                                                                    var text = JSON.parse(res.responseText);
                                                                    if (text.success === true) {
                                                                        store.load();
                                                                        Ext.Msg.alert('提示', text.data);
                                                                    } else {
                                                                        Ext.Msg.alert('提示', text.error);
                                                                    }

                                                                },
                                                                failure: function (res) {
                                                                    Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                                                                }
                                                            })
                                                        }
                                                    }
                                                });
                                            }
                                        });
                                    }
                                    if (Ext.get(id2)) {
                                        Ext.widget('button', {
                                            renderTo: id2,
                                            text: '删除',
                                            scale: 'small',
                                            handler: function () {
                                                Ext.Msg.show({
                                                    icon: Ext.Msg.QUESTION,
                                                    title: '确定操作',
                                                    msg: '是否确定删除' + rec.data.userName + '的申请？',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function (btn, text) {
                                                        if (btn == 'yes') {
                                                            Ext.Ajax.request({
                                                                type: 'rest',
                                                                url: 'rest/audit/delete?id=' + rec.data.id,
                                                                success: function (res) {
                                                                    var text = JSON.parse(res.responseText);
                                                                    if (text.success === true) {
                                                                        store.load();
                                                                        Ext.Msg.alert('提示', text.data);
                                                                    } else {
                                                                        Ext.Msg.alert('提示', text.error);
                                                                    }

                                                                },
                                                                failure: function (res) {
                                                                    Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                                                                }
                                                            })
                                                        }
                                                    }
                                                });
                                            }
                                        });
                                    }
                                }, 100);
                                return Ext.String.format('<span id="{0}"></span><span id="{1}"></span>', id, id2);
                            }
                        },
                    ],
                    features: [{
                        ftype: 'grouping',
                        groupHeaderTpl: new Ext.XTemplate(
                            '<tpl if="name == 0">',
                            '未审核',
                            '</tpl>',
                            '<tpl if="name == 1">',
                            '未通过',
                            '</tpl>',
                            '<tpl if="name == 2">',
                            '已批准',
                            '</tpl>'
                        )
                    }],
                    listeners: {
                        render: function (me) {
                            var store = me.getStore();
                            //store.load();
                        }
                    }
                }
            ]
        },
        {
            xtype: 'collapsibleBlock',
            title: '待审批的服务',
            items: [
                {
                    xtype: 'grid',
                    store: Ext.create('cfWeb.store.serviceAudit.ServiceAuditStore'),
                    columns: [
                        {text: '编号', dataIndex: 'id', hidden: true},
                        {text: '服务实例', dataIndex: 'serviceInstanceName', flex: 1.4},
                        {text: '服务名称', dataIndex: 'service', flex: 1},
                        {text: '服务计划', dataIndex: 'plan', flex: 1.5},
                        {text: '组织', dataIndex: 'org', flex: 0.8},
                        {text: '空间', dataIndex: 'space', flex: 0.8},
                        {text: '日期', dataIndex: 'createTime', flex: 1.5},
                        {
                            flex: 2,
                            text: '操作',
                            renderer: function (val, meta, rec) {
                                // generate unique id for an element
                                var id = Ext.id();
                                var id2 = Ext.id();
                                var store = this.getStore();
                                var state = rec.data.state;
                                Ext.defer(function () {
                                    if ((state == 0||state == 1)&&Ext.get(id)) {
                                        Ext.widget('button', {
                                            renderTo: id,
                                            text: '批准',
                                            scale: 'small',
                                            handler: function () {
                                                Ext.Msg.show({
                                                    icon: Ext.Msg.QUESTION,
                                                    title: '确定操作',
                                                    msg: '是否确定批准' + rec.data.serviceInstanceName + '的申请？',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function (btn, text) {
                                                        if (btn == 'yes') {
                                                            var waitWindow = Ext.MessageBox.show({
                                                                title: cfWebLang.Util.CreateApp,
                                                                closable: false,
                                                                cls: 'style-cfLoading'
                                                            });
                                                            Ext.Ajax.request({
                                                                type: 'rest',
                                                                url: 'rest/serviceaudit/agree?id=' + rec.data.id,
                                                                success: function (res) {
                                                                    var text = JSON.parse(res.responseText);
                                                                    if (text.success === true) {
                                                                        store.load();
                                                                        Ext.Msg.alert('提示', text.data);
                                                                    } else {
                                                                        Ext.Msg.alert('提示', text.error);
                                                                    }
                                                                    waitWindow.close();
                                                                },
                                                                failure: function (res) {
                                                                    Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                                                                    waitWindow.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                });
                                            }
                                        });
                                    }
                                    if ((state == 0)&&Ext.get(id2)) {
                                        Ext.widget('button', {
                                            renderTo: id2,
                                            text: '拒绝',
                                            scale: 'small',
                                            handler: function () {
                                                Ext.Msg.show({
                                                    icon: Ext.Msg.QUESTION,
                                                    title: '确定操作',
                                                    msg: '是否确定拒绝' + rec.data.serviceInstanceName + '的申请？',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function (btn, text) {
                                                        if (btn == 'yes') {
                                                            Ext.Ajax.request({
                                                            type: 'rest',
                                                            url: 'rest/serviceaudit/disagree?id=' + rec.data.id,
                                                            success: function (res) {
                                                                var text = JSON.parse(res.responseText);
                                                                if (text.success === true) {
                                                                    store.load();
                                                                    Ext.Msg.alert('提示', text.data);
                                                                } else {
                                                                    Ext.Msg.alert('提示', text.error);
                                                                }

                                                            },
                                                            failure: function (res) {
                                                                Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                                                            }
                                                        })
                                                    }
                                                    }
                                                });
                                            }
                                        });
                                    }
                                    if (Ext.get(id2)) {
                                        Ext.widget('button', {
                                            renderTo: id2,
                                            text: '删除',
                                            scale: 'small',
                                            handler: function () {
                                                Ext.Msg.show({
                                                    icon: Ext.Msg.QUESTION,
                                                    title: '确定操作',
                                                    msg: '是否确定删除' + rec.data.serviceInstanceName + '的申请？',
                                                    buttons: Ext.Msg.YESNO,
                                                    fn: function (btn, text) {
                                                        if (btn == 'yes') {
                                                            Ext.Ajax.request({
                                                                type: 'rest',
                                                                url: 'rest/serviceaudit/delete?id=' + rec.data.id,
                                                                success: function (res) {
                                                                    var text = JSON.parse(res.responseText);
                                                                    if (text.success === true) {
                                                                        store.load();
                                                                        Ext.Msg.alert('提示', text.data);
                                                                    } else {
                                                                        Ext.Msg.alert('提示', text.error);
                                                                    }

                                                                },
                                                                failure: function (res) {
                                                                    Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                                                                }
                                                            })
                                                        }
                                                    }
                                                });
                                            }
                                        });
                                    }
                                }, 100);
                                return Ext.String.format('<span id="{0}"></span><span id="{1}"></span>', id, id2);
                            }
                        }
                    ],
                    features: [{
                        ftype: 'grouping',
                        groupHeaderTpl: new Ext.XTemplate(
                            '<tpl if="name == 0">',
                            '未审核',
                            '</tpl>',
                            '<tpl if="name == 1">',
                            '未通过',
                            '</tpl>',
                            '<tpl if="name == 2">',
                            '已批准',
                            '</tpl>'
                        )
                    }],
                    listeners: {
                        render: function (me) {
                            var store = me.getStore();
                            //store.load();
                        }
                    }
                }
            ]
        }
    ]
});
